﻿Public Class ProjektManagerImpl
    Inherits ManagerImpl(Of Projekt)
    Implements ProjektManager

    Private Class ProjektMapper
        Implements RowMapper(Of Projekt)
        Public Function map(ByRef reader As System.Data.OleDb.OleDbDataReader) As Projekt Implements RowMapper(Of Projekt).map
            Dim projekt As New Projekt()
            projekt.id = reader("id")
            projekt.name = reader("name")
            Return (projekt)
        End Function
    End Class

    Sub New()
        MyBase.New("projekt", New ProjektMapper())
    End Sub

    Public Function findByKunde(ByRef kunde As Kunde) As System.Collections.Generic.List(Of Projekt) Implements ProjektManager.findByKunde
        Dim query As New Query()
        query.sql = "select * from " & tableName & " where kunde_id=:kundeId"
        query.setParameter("kundeId", kunde.id)
        Return selectQuery(query, New ProjektMapper())
    End Function

    Public Sub deleteByKunde(ByRef kunde As Kunde) Implements ProjektManager.deleteByKunde
        Dim query As New Query()
        query.sql = "delete from " & tableName & " where kunde_id=:kundeId"
        query.setParameter("kundeId", kunde.id)
        runQuery(query)
    End Sub

    Public Overrides Sub insert(ByRef entity As Projekt)
        Dim query As New Query()
        query.sql = "insert into " & tableName & " (name, kunde_id) values (:name, :kundeId)"
        query.setParameter("name", entity.name)
        query.setParameter("kundeId", entity.kunde.id)
        runQuery(query)
    End Sub

    Public Overrides Sub update(ByRef entity As Projekt)
        Dim query As New Query()
        query.sql = "update " & tableName & " set name=:name, kunde_id=:kundeId where id=:id"
        query.setParameter("name", entity.name)
        query.setParameter("kundeId", entity.kunde.id)
        query.setParameter("id", entity.id)
        runQuery(query)
    End Sub
End Class
